package com.zw.yzk.kabu.skin;

import android.content.Context;
import com.google.gson.reflect.TypeToken;
import com.zw.yzk.kabu.data.utils.FileUtils;
import com.zw.yzk.kabu.data.utils.SharedPrefrenceUtils;
import com.zw.yzk.kabu.model.SkinModel;
import skin.support.SkinCompatManager;
import skin.support.utils.SkinFileUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by skydar on 2017/4/15.
 */

public class LocalSkinManager {

    public static final String KEY_LOCAL_SKIN_MAP = "key_local_skin_map";
    public static final String KEY_CURRENT_SKIN = "key_current_skin";

    private Context context;
    private Map<Integer, String> skinMap ; // key id, value name;
    private String skinDir;


    private LocalSkinManager(){

    }

    private static LocalSkinManager instance = new LocalSkinManager();

    public static LocalSkinManager getInstance(){
        instance.check();
        return instance;
    }

    public void init(Context context){
        this.context = context;
        skinMap = SharedPrefrenceUtils.getObject(context, KEY_LOCAL_SKIN_MAP, new TypeToken<Map<Integer, String>>() {}.getType());
        if(skinMap == null){
            skinMap = new HashMap<>();
        }
        skinDir = SkinFileUtils.getSkinDir(context);
        File dir = new File(skinDir);
        if(!dir.exists()) dir.mkdirs();
    }

    public void check(){
        if(skinMap == null) return;
        boolean changed = false;
        Iterator<Map.Entry<Integer, String>> it = skinMap.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<Integer, String> entry = it.next();
            String path = entry.getValue();
            File file = new File(path);
            if(!file.exists()){
                changed = true;
                it.remove();
            }
        }
        if(changed && context != null){
            SharedPrefrenceUtils.saveObject(context, KEY_LOCAL_SKIN_MAP, skinMap);
        }
    }

    public String getSkinDir(){
        return skinDir;
    }

    public String generateName(int id, String name){
        return id + name + ".skin";
    }

    public File getSkinTempFile(SkinModel skin){
        return new File(getSkinDir() + File.separator + generateName(skin.getId(), skin.getName()));
    }

    public String getSkinName(int id){
        String file = skinMap.get(id);
        if(file == null) return null;
        return file.substring(file.lastIndexOf(File.separator)+1);
    }

    public File getSkinFile(int id){
        String file = skinMap.get(id);
        if(file == null) return null;
        return new File(file);
    }

    public void addSkin(int id, File skin){
        skinMap.put(id, skin.getAbsolutePath());
        SharedPrefrenceUtils.saveObject(context, KEY_LOCAL_SKIN_MAP, skinMap);
    }

    public void deleteSkin(int id){
        File file = getSkinFile(id);
        if(file != null || file.exists()){
            file.delete();
        }
        skinMap.remove(id);
        SharedPrefrenceUtils.saveObject(context, KEY_LOCAL_SKIN_MAP, skinMap);
    }

    public void currentSkin(int id){
        SharedPrefrenceUtils.setValue(context, KEY_CURRENT_SKIN, id);
    }

    public String getCurrentSkin(){
        return SkinCompatManager.getInstance().getCurSkinName();
    }

}
